using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;

namespace Freshbooks.Client
{
    public class ClientCommand
    {
        public static string GetCreateXml(Client client)
        {
            string input;
            using (MemoryStream w = new MemoryStream())
            {
                using (var writer = new XmlTextWriter(w, Encoding.UTF8))
                {
                    writer.WriteStartDocument();
                    writer.WriteStartElement("request");
                    writer.WriteStartAttribute("method");
                    writer.WriteValue("client.create");
                    writer.WriteEndAttribute();
                    writer.WriteStartElement("client");
                    writer.WriteStartElement("first_name");
                    writer.WriteValue(client.FirstName);
                    writer.WriteEndElement();
                    writer.WriteStartElement("last_name");
                    writer.WriteValue(client.LastName);
                    writer.WriteEndElement();
                    writer.WriteStartElement("organization");
                    writer.WriteValue(client.Organization);
                    writer.WriteEndElement();
                    writer.WriteStartElement("email");
                    writer.WriteValue(client.Email);
                    writer.WriteEndElement();
                    writer.WriteStartElement("username");
                    writer.WriteValue(client.Username);
                    writer.WriteEndElement();
                    writer.WriteStartElement("password");
                    writer.WriteValue(client.Password);
                    writer.WriteEndElement();
                    writer.WriteStartElement("work_phone");
                    writer.WriteValue(client.WorkPhone);
                    writer.WriteEndElement();
                    writer.WriteStartElement("home_phone");
                    writer.WriteValue(client.HomePhone);
                    writer.WriteEndElement();
                    writer.WriteStartElement("mobile");
                    writer.WriteValue(client.Mobile);
                    writer.WriteEndElement();
                    writer.WriteStartElement("fax");
                    writer.WriteValue(client.Fax);
                    writer.WriteEndElement();
                    writer.WriteStartElement("notes");
                    writer.WriteValue(client.Notes);
                    writer.WriteEndElement();
                    writer.WriteStartElement("p_street1");
                    writer.WriteValue(client.PStreet1);
                    writer.WriteEndElement();
                    writer.WriteStartElement("p_street2");
                    writer.WriteValue(client.PStreet2);
                    writer.WriteEndElement();
                    writer.WriteStartElement("p_city");
                    writer.WriteValue(client.PCity);
                    writer.WriteEndElement();
                    writer.WriteStartElement("p_state");
                    writer.WriteValue(client.PState);
                    writer.WriteEndElement();
                    writer.WriteStartElement("p_country");
                    writer.WriteValue(client.PCountry);
                    writer.WriteEndElement();
                    writer.WriteStartElement("p_code");
                    writer.WriteValue(client.PCode);
                    writer.WriteEndElement();
                    writer.WriteStartElement("s_street1");
                    writer.WriteValue(client.SStreet1);
                    writer.WriteEndElement();
                    writer.WriteStartElement("s_street2");
                    writer.WriteValue(client.SStreet2);
                    writer.WriteEndElement();
                    writer.WriteStartElement("s_city");
                    writer.WriteValue(client.SCity);
                    writer.WriteEndElement();
                    writer.WriteStartElement("s_state");
                    writer.WriteValue(client.SState);
                    writer.WriteEndElement();
                    writer.WriteStartElement("s_country");
                    writer.WriteValue(client.SCountry);
                    writer.WriteEndElement();
                    writer.WriteStartElement("s_code");
                    writer.WriteValue(client.SCode);
                    writer.WriteEndElement();
                    writer.WriteEndElement();
                    writer.WriteEndDocument();
                    writer.Flush();
                }
                input = Encoding.UTF8.GetString(w.GetBuffer());
                w.Close();
            }
            Regex.Replace(input, "<.* />", "");
            return input;
        }
    }
}